//
//  TTDatabase.h
//  Tint
//
//  Created by Kevin Wu on 2/28/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <sqlite3.h>
#import "Core/Core.h"
#import "Additions/Additions.h"



@class TTDatabaseResult;

@interface TTDatabase : NSObject {
    sqlite3 *_handle;
    NSString *_path;
    
	BOOL _opened;
    
	NSLock *_executeLock;
}

@property(nonatomic, readonly) sqlite3 *handle;
@property(nonatomic, readonly) NSString *path;


- (id)initWithPath:(NSString *)path;

/*
 * Singleton
 */
+ (TTDatabase *)sharedObject;
+ (void)setSharedObject:(TTDatabase *)object;


/*
 * Sqlite connection
 */
- (BOOL)open;
- (void)close;
- (BOOL)goodConnection;

/*
 * Execute update
 */
- (BOOL)executeUpdateString:(NSString *)sql;
- (BOOL)executeUpdate:(NSString *)sql, ...;
- (BOOL)executeUpdate:(NSString *)sql parameters:(NSArray *)parameters;

/*
 * Execute insert
 */
- (sqlite3_int64)executeInsertString:(NSString *)sql;
- (sqlite3_int64)executeInsert:(NSString *)sql, ...;
- (sqlite3_int64)executeInsert:(NSString *)sql parameters:(NSArray *)parameters;

/*
 * Execute query
 *
 * Between int [db newResultByExecutingQuery:@"select * from test where a>? and a<?", 
 *                                          [NSNumber numberWithInt:3], [NSNumber numberWithInt:10], nil];
 *             [db newResultByExecutingQueryString:@"select * from test where a>3 and a<10"];
 * 
 * String equal [db newResultByExecutingQuery:@"select * from test where b=?", @"number 3", nil];
 *              [db newResultByExecutingQueryString:@"select * from test where b='number 3'"];
 * 
 * Date check [db newResultByExecutingQuery:@"select * from test where d>?", fromDate, nil];
 */
- (TTDatabaseResult *)newResultByExecutingQueryString:(NSString *)sql;
- (TTDatabaseResult *)newResultByExecutingQuery:(NSString *)sql, ...;
- (TTDatabaseResult *)newResultByExecutingQuery:(NSString *)sql parameters:(NSArray *)parameters;

/*
 * Error methods
 */
- (NSString *)lastErrorMessage;
- (int)lastErrorCode;
- (BOOL)hadError;

/*
 * Misc
 */
+ (BOOL)isSQLiteThreadSafe;
+ (NSString *)sqliteLibVersion;
- (BOOL)hasTable:(NSString *)tableName;
- (NSUInteger)fileSize;

@end
